<template>
  <div class="login-wrap" :style="{backgroundImage:'url(static/img/login_bg.png)'}">
    <div class="content">
        <div class="login-box">
            <div class="loginTop">
                <img src="static/img/loginLogo.png" alt="logo">
                <span class="title">{{ title }}</span>
            </div>
            <el-form v-show="!isConfirmIdentity">
                <label class="input-wrapper">
                    <input type="text" class="input-css" placeholder="登陆账号" v-model="userName">
                </label>
                <label class="input-wrapper input-code">
                    <input type="text" class="input-css" placeholder="输入短信验证码" v-model="msgCode" maxlength="6">
                    <div class="mesCode">
                        <button @click="getCode" :disabled="disabled" :class="{'unChoose': disabled}">{{btnTitle}}</button>
                    </div>
                </label>
            </el-form>
            <el-form v-show="isConfirmIdentity" :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-form-item prop="userPwd">
                    <el-input type="password" v-model="ruleForm.userPwd" autocomplete="off" class="input-css" placeholder="新密码"></el-input>
                </el-form-item>
                <el-form-item prop="confirmUserPwd">
                    <el-input type="password" v-model="ruleForm.confirmUserPwd" autocomplete="off" class="input-css" placeholder="确认密码"></el-input>
                </el-form-item>
                <!-- <label class="input-wrapper">
                    <input type="password" class="input-css" placeholder="新密码" v-model="ruleForm.userPwd" onpaste="return false">
                </label>
                <label class="input-wrapper">
                    <input type="password" class="input-css" placeholder="确认密码" v-model="ruleForm.confirmUserPwd" onpaste="return false">
                </label> -->
            </el-form>
            <button type="submit" class="login-btn" @click="next" v-show="!isConfirmIdentity">确 认 修 改</button>
            <button type="submit" class="login-btn" @click="reset('ruleForm')" v-show="isConfirmIdentity">重 置 密 码</button>
        </div>
    </div>
    <!-- <div class="bottom">
        <div>江苏移动版权所有 苏ICP备11070397号-933 copyright ©</div>
        <div>江苏移动和教育 中国移动通信集团江苏有限公司 ALL Rights Reserved 版权所有</div>
    </div> -->
  </div>
</template>


<script>
import Storage from '@/base/storage.js'
import Identify from 'components/Identify.vue'

export default {
    components: { Identify },
    data(){
        var validatePass = (rule, value, callback) => {
            let passwordReg = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,20}');
            if (!passwordReg.test(value)) {
                callback(new Error('密码必须包含字母、数字和特殊符号,长度八位到二十位'));
            } else {
            if (this.ruleForm.confirmUserPwd !== '') {
                this.$refs.ruleForm.validateField('checkPass');
            }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.userPwd) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            userName:'', //账号
            btnTitle: "获取验证码",
            disabled:false,  //是否可点击
            choosed: false,
            msgCode: "",
            title: "身份验证",
            isConfirmIdentity: false,
            rules: {
                userPwd:[{validator:validatePass,trigger: 'blur'}],
                confirmUserPwd:[{validator:validatePass2,trigger: 'blur'}]
            },
            ruleForm: {
                userPwd: '',
                confirmUserPwd: '',
            },
        };
    },
    methods:{
        //获取验证码
        getCode() {
            this.disabled = true;
            if(this.userName.trim() != "") {
                let url = "/lbs/usercenter/login/h5SendCode";
                let params = {
                    userAcct: this.userName,
                    operType: "RS",
                }
                this.$http.post(url, params).then(res => {
                    if(res.data.retCode == "0") {
                        this.$message({
                            message: '验证码已发送',
                            type: 'success'
                        });
                        console.log(res);
                        //倒计时
                        let time = 60;
                        let timer = setInterval(() => {
                            if(time == 0) {
                                clearInterval(timer);
                                this.disabled = false;
                                this.btnTitle = "获取验证码";
                            } else {
                                this.btnTitle =time + '秒后重试';
                                this.disabled = true;
                                time--
                            }
                        },1000)
                    }else {
                        this.disabled = false;
                        this.$message({
                            message: res.data.retMsg,
                            type: 'warning'
                        });
                    }
                })
                .catch(response => {
                    this.disabled = false;
                    this.$message({
                        message: response,
                        type: 'error'
                    });
                })
            }else if(this.userName == ""){
                this.disabled = false;
                this.$message({
                    message: "请输入登陆账号",
                    type: 'warning'
                });
            }
        },
        //确认修改密码
        confirmChange() {
            if(this.userName.trim()) {
                this.$message({
                    type: "error",
                    message: "请输入登陆账号!",
                });
            }else if(this.userPwd.trim() == "") {
                this.$message({
                    type: "error",
                    message: "请输入新密码!",
                });
            }else if(this.confirmUserPwd.trim() == "") {
                this.$message({
                    type: "error",
                    message: "请输入确认密码!",
                })
            }else if(this.msgCode.trim() == "") {
                this.$message({
                    type: "error",
                    message: "请输入验证码",
                })
            }else if(this.userPwd.trim() != this.confirmUserPwd.trim()) {
                this.$message({
                    type: "error",
                    message: "新密码和确认密码不一致!",
                })
            }
        },
        next() {
            let url = "/lbs/usercenter/login/h5JudgeCode";
            let params = {
                userAcct: this.userName,
                operType: "RS",
                code: this.msgCode
            }
            this.$http.post(url, params).then(res => {
                let {retCode,retMsg,data} = res.data;
                if (retCode == '0') {//成功的情况
                    this.isConfirmIdentity = true;
                    this.title = "修改密码";
                    this.userPwd = "";
                    Storage.session.set('token', data.token);
                } else {
                    this.$alert(retMsg || '请求失败');
                }
            }).catch(err => {
              this.$alert(err)
            })
        },
        //重置密码
        reset(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let url = "/lbs/usercenter/login/h5ResetPwd";
                    let params = {
                        userAcct: this.userName,
                        userPassword: this.ruleForm.confirmUserPwd
                    }
                    this.$http.post(url, params).then(res => {
                        let {retCode,retMsg,data} = res.data;
                        if (retCode == '0') {//成功的情况
                            this.$message({
                                message: retMsg,
                                type: 'success'
                            });
                            this.$router.push('login');
                        } else {
                        this.$alert(retMsg || '请求失败');
                        }
                    }).catch(err => {
                        this.$alert(err)
                    })
                } else {
                    return false;
                }
            });
        }
    },
    mounted() {

    }
}
</script>

<style scoped lang="less">
button {
    outline: none;
}
.login-wrap{
    position: absolute;
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    flex-direction:column;
    justify-content:center;
    align-items:center;
    text-align: center;
}
.bottom {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
    line-height: 30px;
    position: absolute;
    bottom: 20px;
}
input {
    outline: none;
}
.content {
    width: 400px;
    border:1px solid #ccc;
    border-radius: 16px;
    background-color: #fff;
    box-shadow: 4px 4px 8px #ccc;
    position: absolute;
    left: 60%;
}
.login-box{
    flex: 1;
    padding: 36px;
    .input-wrapper {
        display: flex;
        font-size: 14px;
        width: 100%;
        height: 48px;
        padding: 0;
        color: #8590a6;
        border: none;
        border-bottom: 1px solid #ebebeb;
        border-radius: 0;
    .input-css{
        flex: 1 1;
        padding: 0 16px;
        overflow: hidden;
        background: transparent;
        border: none;
        resize: none;
        color: #1a1a1a;
    }
}
.input-code {
    border-bottom: 0;
    display: flex;
    align-items: center;
    height: 52px;
    justify-content: space-between;
    margin-top: 5px;
    .input-css{
        flex: 0.9;
        padding: 0 16px;
        overflow: hidden;
        background: #F6F6F6;
        border-radius: 12px;
        height: 40px;
    }
    input::placeholder {
        color: #999999;
        font-size: 12px;
    }
    .identify {
        height: 38px;
        border-radius: 12px;
        overflow: hidden;
    }
}
.input-mesCode {
    height: 48px;
}
.mesCode {
    height: 48px;
    button {
        margin-top: 7px;
        width: 100px;
        height: 38px;
        border: none;
        color: #fff;
        background-color: #1292F0;
        border-radius: 12px;
    }
    .unChoose {
        background-color: rgba(239, 239, 239, 0.3);
        color: #999999;
    }
}
.login-btn{
    width: 100%;
    margin-top: 20px;
    height: 36px;
    display: inline-block;
    padding: 0 16px;
    font-size: 14px;
    line-height: 32px;
    color: #fff;
    text-align: center;
    cursor: pointer;
    background: none;
    border: 1px solid;
    border-radius: 24px;
    border-color: #0084ff;
    background-color: #0084ff;
  }
}
.agreement {
    color: #737373;
    font-size: 12px;
    text-align: left;
    display: flex;
    margin-top: 12px;
    .loginAgreement {
        line-height: 20px;
    }
    .choose {
        display: inline-block;
        width: 16px;
        height: 13px;
        border: 1px solid #737373;
        margin: 3px 10px 0 0;
        line-height: 13px;
        i {
            font-weight: 700;
            color: #1292F0;
        }
    }
}
.loginTop {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 15px;
    img {
        width: 37px;
        height: auto;
    }
    .title {
        font-size: 28px;
        color: #296EB8;
        margin-left: 5px;
    }
}
</style>